package org.fall.mvc.handler;

import com.github.pagehelper.PageInfo;
import crowd.entity.Admin;
import org.fall.constant.CrowdConstant;
import org.fall.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author XZ
 * @create 2021-12-01 11:05
 */
@Controller
public class AdminHandle {
    @Autowired
    private AdminService adminService;

    @RequestMapping("admin/page/doUpdate.html")
    public String doUpdate(Admin admin,@RequestParam("pageNum") Integer pageNum,@RequestParam("keyword") Integer keyword) {
        adminService.updateAdmin(admin);
        return "redirect:/admin/page/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    @RequestMapping("admin/page/update/{adminId}/{pageNum}/{keyword}.html")
    public String toUpdatePage(@PathVariable("adminId") Integer adminId,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("keyword") String keyword,
                              ModelMap modelMap) {
        Admin admin = adminService.queryAdmin(adminId);

        modelMap.addAttribute("admin", admin);
        modelMap.addAttribute("pageNum", pageNum);
        modelMap.addAttribute("keyword", keyword);

        return "admin-update";
    }

    @RequestMapping("/admin/page/doSave.html")
    public String addAdmin(Admin admin) {
        adminService.save(admin);
        return "redirect:/admin/page/page.html?pageNum=" + Integer.MAX_VALUE;
    }

    @RequestMapping("/admin/page/remove/{adminId}/{pageNum}/{keyword}.html")
    public String removeAdmin(@PathVariable("adminId")Integer adminId,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("keyword") String keyword) {
        adminService.removeById(adminId);
        return "redirect:/admin/page/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    @RequestMapping("/admin/page/page.html")
    public String getAdminPage(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                               @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                               ModelMap map) {
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        map.addAttribute(CrowdConstant.NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }

    @RequestMapping("admin/login/login.html")
    public String doLogin(
            @RequestParam("login-user") String userName,
            @RequestParam("login-pwd") String password,
            HttpSession session
    ) {
        Admin admin = adminService.getAdminByUsername(userName, password);
        session.setAttribute(CrowdConstant.LOGIN_ADMIN_NAME, admin);
        return "redirect:/admin/to/main/page.html";
    }

    @RequestMapping("admin/login/logout.html")
    public String doLogout(HttpSession session) {
        //session失效
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }
}